package com.tao.erp.emp.action;



import java.util.HashMap;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tao.erp.emp.pojo.Emp;
import com.tao.erp.emp.service.EmpService;
import com.tao.erp.utils.format.MD5Utils;

@Controller
@RequestMapping("/emp")
public class EmpAction {

	@Autowired
	private EmpService empService;
	
	/**
	 * 验证cookie
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping("/cookieCheck.action")
	public String cookieCheck(HttpServletRequest request,HttpServletResponse response,Model model){
		Emp emp = new Emp();
		Cookie[] cookies = request.getCookies();
		for (Cookie cookie : cookies) {
			if(cookie.getName().equals("cookieLoginEmp")){
				String value = cookie.getValue();
				String[] split = value.split(":");
				emp.setUserName(split[0]);
				emp.setPwd(split[1]);
			}
		}
		if(emp.getUserName()==null){
			return "/jsp/login.jsp";
		}
		Emp emp2 = empService.selectEmpByUserName(emp);
		model.addAttribute("loginEmp", emp);
		if(emp2==null){
			model.addAttribute("loginMsg", "用户名不存在");
			return "/jsp/login.jsp";
		}
		if(!emp.getPwd().equals(emp2.getPwd())){
			model.addAttribute("loginMsg", "密码错误");
			return "/jsp/login.jsp";
		}
		HttpSession session = request.getSession();
		session.setAttribute("sessionEmp", emp2);
		
		return "/jsp/index.jsp";
	}
	
	
	/**
	 * 登录用户
	 * @param emp
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/login.action")
	public String login(Emp emp,String remember,Model model,HttpServletRequest request,HttpServletResponse response){
		emp.setPwd(MD5Utils.md5(emp.getPwd()));
		Emp emp2 = empService.selectEmpByUserName(emp);
		model.addAttribute("loginEmp", emp);
		if(emp2==null){
			model.addAttribute("loginMsg", "用户名不存在");
			return "/jsp/login.jsp";
		}
		if(!emp.getPwd().equals(emp2.getPwd())){
			model.addAttribute("loginMsg", "密码错误");
			return "/jsp/login.jsp";
		}
		HashMap<String,String> map = new HashMap<String, String>();
		map.put("1", "送货");
		map.put("2", "自提");
		HttpSession session = request.getSession();
		session.setAttribute("sessionEmp", emp2);
		session.setAttribute("sessionMap", map);
		if(remember!=null){
			Cookie cookie = new Cookie("cookieLoginEmp",emp2.getUserName()+":"+emp2.getPwd());  
			cookie.setMaxAge(60*60);          
			response.addCookie(cookie);
		}
		return "/jsp/index.jsp";
	}
	@RequestMapping("/loginOut.action")
	public String loginOut(HttpServletRequest request,Model model){
		HttpSession session = request.getSession();
		session.invalidate();
		model.addAttribute("loginMsg","用户已注销" );
		return "/jsp/login.jsp";
	}
	
	@RequestMapping("/selectEmpList.action")
	@ResponseBody
	public List<Emp> selectEmpList(Emp emp){
		List<Emp> list = empService.selectEmpList(emp);
		return list;
	}
	
}
